package com.emip.servlet.data;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;

import com.emip.bean.ExpFlagBean;
import com.emip.bean.SiteItemBean;
import com.emip.bean.Store;
import com.emip.pojo.Report;
import com.emip.service.AlterLogService;
import com.emip.service.ExpFlagService;
import com.emip.service.SiteItemService;
import com.emip.util.ReportFormat;
import com.emip.util.Util;
import com.emip.util.report.Statistics;



public class DataManage {
	
	@Autowired
	SiteItemService siteItemService;
	
	@Autowired
	AlterLogService alterLogService;
	
	@Autowired
	protected ExpFlagService expFlagService;
	
	protected ModelMap mmap = new ModelMap();

	protected ModelMap setDefaultParams(ModelMap mmap) {
		if (mmap == null) mmap = new ModelMap();
		String curName = this.getClass().getSimpleName();
		curName = curName.substring(0, curName.indexOf("DataAction"));
		mmap.put("type", curName.toLowerCase());
		
		List<ExpFlagBean> flags = expFlagService.getAll();
		Map<String, ExpFlagBean> flagMap = new HashMap<String, ExpFlagBean>();
		for (int i = 0; i < flags.size(); i++) {
			ExpFlagBean flag = flags.get(i);
			flagMap.put(Util.trim(flag.getFlag()), flag);
		}
		mmap.put("flags", Util.Object2String(flagMap));
		
		return mmap;
	}
	
	protected <T> Report formatReportBody(List<T> datas) {
		ReportFormat reportFormat = new ReportFormat();
		reportFormat.appendBodyData(datas);
		
		return reportFormat.getReport();
	}
	
	protected void setFlag(Store store) {
		Double value = Double.parseDouble(store.getValue());
		SiteItemBean siteItem = siteItemService.findSiteItem(store.getSiteID(), store.getItemID());
		if (value <= siteItem.geteMaxValue() && value >= siteItem.geteMinValue()) {
			if (value <= siteItem.getOverMaxValue() && value >= siteItem.getOverMinValue()) {
				store.setFlag(null);
			} else 
				store.setFlag(Statistics.EXCEED_FLAG);
		} else {
			store.setFlag(Statistics.EXCEPTION_FLAG);
		}
	}
	
	protected String[] getItemIds(String siteId) {
		List<SiteItemBean> siteItems = siteItemService.getSiteItemBySiteID(siteId);
		
		String[] itemIds = new String[siteItems.size()];
		for (int i = 0; i < siteItems.size(); i++) {
			itemIds[i] = siteItems.get(i).getItem().getId();
		}
		
		return itemIds;
	}
	
}
